﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;

namespace HYRElectric.WirePlace
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class ChangeAngleCmd : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			ModelUpdater modelUpdater = ModelUpdater.CreateInstance(commandData.Application.ActiveAddInId);
			using (FormCheck formCheck = new FormCheck(UpdaterRegistry.IsUpdaterRegistered(modelUpdater.GetUpdaterId())))
			{
				if (DialogResult.OK == formCheck.ShowDialog())
				{
					if (formCheck.isAddUpdata)
					{
						UpdaterRegistry.RegisterUpdater(modelUpdater);
						ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(Wire));
						UpdaterRegistry.AddTrigger(modelUpdater.GetUpdaterId(), elementClassFilter, Element.GetChangeTypeElementAddition());
					}
					else if (modelUpdater != null)
					{
						UpdaterRegistry.UnregisterUpdater(modelUpdater.GetUpdaterId());
					}
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
